﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuanLyBus.Model;

namespace QuanLyBus
{
    public partial class tram : PhoneApplicationPage
    {
        QL_TuyenDataContext db = new QL_TuyenDataContext(App.constr);
        List<ds_tram> tram_dung = new List<ds_tram>();
        ds_tram t = new ds_tram();
        
        public tram()
        {
            InitializeComponent();
            Load();
        }
        private void list_tram_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                t = list_tram.SelectedItem as ds_tram;
                App.id_t = t.id_tram;
            }
            catch 
            {

                list_tram.SelectedIndex = -1;
            }
            
            
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            if (App.id_t != -1)
                this.NavigationService.Navigate(new Uri("/Sua_tram.xaml", UriKind.Relative));
            else MessageBox.Show("Bạn chưa chọn...");
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            MessageBoxResult rw = MessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButton.OKCancel);
            if (rw == MessageBoxResult.OK)
            {
                xoa();
                MessageBox.Show("xóa thành công");
            }
            
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.NavigationService.GoBack();
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            this.NavigationService.GoBack();
            base.OnBackKeyPress(e);
        }
        private void xoa()
        {
            //MessageBox.Show(App.id_t.ToString());
            var c = db.tram_thuoc_tuyens.Where(p=>p.id_tram == App.id_t);    
            foreach(tram_thuoc_tuyen i in c )
            {
                db.tram_thuoc_tuyens.DeleteOnSubmit(i);
            }
            //db.tram_thuoc_tuyens.DeleteOnSubmit(c);
            //MessageBox.Show(c.ToString());
            db.ds_trams.DeleteOnSubmit(t);
            db.SubmitChanges();
            MessageBox.Show("Xóa Thành công");
            Load();
        }
        private void Load()
        {
            tram_dung = db.ds_trams.ToList();
            this.list_tram.ItemsSource = tram_dung;
        }
    }
}